﻿using Domain.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Extensions;
using Common.Utils;

namespace Domain
{
	public class ExerciseLog : BaseEntity
	{
		public DateTime LogDate { get; set; }
		public List<SetLog> Sets { get; set; }

		public Double MaxWeight()
		{
			if (Sets.IsNullOrEmpty())
				return 0.0;

			return Sets.Max(x => x.Weight);
		}

		public static ExerciseLog GetEmpty()
		{
			return new ExerciseLog() { Sets = new List<SetLog>() };
		}

		public int GetVolume()
		{
			if (Sets.IsNullOrEmpty())
				return 0;

			return (Int32)Sets.Sum(x => x.Weight * x.Repetitions);
		}

        public Boolean IsCompleted()
        {
            return LastSetElapsedTime().TotalMinutes > DateTimeHelper.MAX_EXERCISE_INTERVAL_IN_MINUTES;
        }

        public TimeSpan LastSetElapsedTime()
        {
            if (Sets.IsNullOrEmpty())
                return TimeSpan.FromMinutes(0);

            var lastSetDateTime = Sets.Max(x => x.EndDateTime);
            return DateTimeHelper.Now - lastSetDateTime;
        }
	}
}
